package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.OfflineExamDetailEntity
import com.example.onlineeducation.data.model.OfflineExamRecordDataModel
import com.example.onlineeducation.repository.OfflineRepository
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.collectLatest

class OfflineExamRecordListVM : BaseViewModel() {

    private val mOfflineRepository: OfflineRepository by lazy { OfflineRepository(mApiService) }


    private val _mOfflineExamRecordList = MutableLiveData<PagingData<OfflineExamRecordDataModel>>()
    val mOfflineExamRecordList: LiveData<PagingData<OfflineExamRecordDataModel>> = _mOfflineExamRecordList


    fun getOfflineExamRecordListPager(proId: String,examId:String, personName: String?=null) = ycLaunch {
        mOfflineRepository.getOfflineExamRecordList(proId,examId,personName).cachedIn(viewModelScope).collectLatest {
            _mOfflineExamRecordList.postValue(it)
        }
    }



    private val _mOfflineExamDetails = MutableLiveData<YcResult<OfflineExamDetailEntity?>>()
    val mOfflineExamDetails: LiveData<YcResult<OfflineExamDetailEntity?>> = _mOfflineExamDetails

    fun getOfflineExamDetail(proId: String, examId: String, id: String) = ycLaunch {
        mOfflineRepository.getOfflineExamRecordDetail(proId, examId, id).ycHasLoading().collectLatest {
            _mOfflineExamDetails.postValue(it)
        }
    }
}

